using System;
using System.Collections.Generic;
using System.Linq;

namespace NopSolutions.NopCommerce.LuceneHelper.Tasks
{
    public class SpecificationOptionsUpdateTask : BaseTask<SpecificationOptionsUpdateTask>
    {
        #region Private

        private bool _updateProducts = true;

        #endregion

        #region Properties

        public bool UpdateProducts
        {
            get { return _updateProducts; }
            set { _updateProducts = value; }
        }

        #endregion

        #region Constructors

        public SpecificationOptionsUpdateTask()
            : this(true)
        {

        }

        public SpecificationOptionsUpdateTask(bool updateProducts)
            :base(0)
        {
            UpdateProducts = updateProducts;
        }

        #endregion

        #region Task

        public override void DoTask(IndexManager indexManager, SearchManager searchManager, IndexWriterWrapper indexer)
        {
            foreach (var task in DatabaseHelper.GetAllMappedOptions().ToTasks(UpdateProducts))
            {
                task.DoTask(indexManager, searchManager, indexer);
            }
        }

        public override LuceneIndexType IndexType
        {
            get { return LuceneIndexType.SpecificaitonOptions; }
        }

        public override bool RebuildIndex
        {
            get { return true; }
        }

        public override IEnumerable<Func<SpecificationOptionsUpdateTask, object>> ComparableProperties()
        {
            return new List<Func<SpecificationOptionsUpdateTask, object>> {x => x.UpdateProducts};
        }

        public override void UpdateTask(SpecificationOptionsUpdateTask newTask)
        {
            if (!UpdateProducts)
            {
                UpdateProducts = newTask.UpdateProducts;
            }
        }

        #endregion
    }
}
